<?php
class Index{
    # region APP热更新
    public static function appWgtHotUpdate($paras)
    {
        global $db;
        global $utility;

        //需要处理的页面参数，用来翻页操作及生成缓存键
        $url_args = array(
            'cmd',
            'token',
            'remoteip',
        );
        $paras = $utility->get_url_args($paras, $url_args);

        $result = array(
            'state' => 200,
            'version' => $utility->getSystemParams('version'),
            'updateurl' => $utility->getSystemParams('appupdateurl'),
            'iosaudit' => $utility->getSystemParams('iosaudit'),
            'helpid' => $utility->getSystemParams('helpid'),
        );
        return $result;
    }
    # endregion

    # region 生成二维码并返回路径
    public static function markQrcode($paras)
    {
        global $utility;

        //需要处理的页面参数，用来翻页操作及生成缓存键
        $url_args = array(
            'cmd',
            'token',
            'l|N|0',//是否在中间显示LOGO
            'c|S|error', //内容
            'q|S|L',//二维码的容错率
            's|N|6',//图片的质量
            'uno',
            'remoteip',
        );
        $paras = $utility->get_url_args($paras, $url_args);
        # 保存的文件名
        $file_dir = date('Ymd',time());
        $backFileName = 'qrcode/' . $file_dir . '/qrcode_' . $paras['uno'] . '.png';
        $qrCodeFileName = __DIR__ . Config\Sys::$imgs_upload_dir . 'qrcode/' . $file_dir . '/qrcode_' . $paras['uno'] . '.png';

        $new_file_dir = __DIR__ . Config\Sys::$imgs_upload_dir . "qrcode/".$file_dir."/";
        if(!file_exists($new_file_dir))
        {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file_dir, 0755);
            chgrp($new_file_dir, Config\Sys::$sysrun_group);
            chown($new_file_dir, Config\Sys::$sysrun_user);
        }

        if ($paras['l'] == 1){
            QRcode::png($paras['c'], $qrCodeFileName, $paras['q'], $paras['s']);
            $logo = __DIR__ . Config\Sys::$imgs_upload_dir . 'qr_logo.png'; // 准备好的logo图片

            if ($logo !== FALSE) {
                $QR   = $qrCodeFileName; // 已经生成的原始二维码图
                $QR             = imagecreatefromstring(file_get_contents($QR));   // 从字符串中的图像流新建一图像
                $logo           = imagecreatefromstring(file_get_contents($logo)); // 从字符串中的图像流新建一图像
                $QR_width       = imagesx($QR);   // 二维码图片宽度
                $QR_height      = imagesy($QR);   // 二维码图片高度
                $logo_width     = imagesx($logo); // logo图片宽度
                $logo_height    = imagesy($logo); // logo图片高度
                // 把logo等比例缩放
                $logo_qr_width  = $QR_width / 5;
                $scale          = $logo_width / $logo_qr_width;
                $logo_qr_height = $logo_height / $scale;
                $from_width     = ($QR_width - $logo_qr_width) / 2;
                //重新组合图片并调整大小
                imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
                    $logo_qr_height, $logo_width, $logo_height);
            }
            //header('Content-type: image/png');
            //输出图片
            //imagepng($QR);
            // 或者保存图片 imagepng($QR, '保存路径');
            imagepng($QR, $qrCodeFileName);
        }else{
            QRcode::png($paras['c'], $qrCodeFileName, $paras['q'], $paras['s']);
        }

        chgrp($qrCodeFileName, Config\Sys::$sysrun_group);
        chown($qrCodeFileName, Config\Sys::$sysrun_user);

        return array(
            'state' => 200,
            'cmd' => $paras['cmd'],
            'qrcode' => $backFileName,
            'uno' => $paras['uno'],
            'url' => $utility->getSystemParams('registerurl'),
        );
    }
    # endregion

    # region 排行榜
    public static function rankingForDay($paras)
    {
        global $utility;
        global $redis;
        global $db;

        $data = array();

        $redisKey = \Config\Sys::$redisKey['RANKING'].'day';
        $data = $utility->redisGet($redisKey);
        if(empty($data)){
            $nowtime = date("Y-m-d",time());
            $beginTime = strtotime($nowtime);
            $endTime = $beginTime + 86400;


            $tablename = \Config\Sys::$dbprefix.'autobetslog l,'.\Config\Sys::$dbprefix.'users u';
            $fields = 'l.uid,u.nickname,u.avatar,sum(l.profit) as profits';
            $where = 'l.`state`=1 AND l.`uid`=u.`uid` AND (l.`times` BETWEEN '.$beginTime.' AND '.$endTime.')';
            $gho = 'GROUP BY l.uid HAVING (profits>0) ORDER BY profits DESC LIMIT 20';

            $uniontablename = \Config\Sys::$dbprefix.'betslog l,'.\Config\Sys::$dbprefix.'users u';

            $unionsql = "select {$fields} from  {$uniontablename} WHERE {$where}";

            $sql = "select {$fields} from {$tablename} WHERE {$where} GROUP BY l.uid HAVING (profits>0) union {$unionsql} {$gho}";

            $data = $db->query($sql);

            # 统计时间 - 缓存5分钟
            $utility->redisSetEx($redisKey, $data, 1800);
        }
        return array(
            'state' => 200,
            'datas' => $data,
        );
    }
    # 本周排行榜
    public static function rankingForWeek($paras)
    {
        return array(
            'state' => 200,
            'datas' => array(),
        );
        /*
        global $utility;
        global $redis;
        global $db;

        $data = array();

        $redisKey = \Config\Sys::$redisKey['RANKING'].'week';

        $data = $utility->redisGet($redisKey);

        if(empty($data)){
            $beginTime = date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
            $endTime = date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));

            $beginTime = strtotime($beginTime);
            $endTime = strtotime($endTime);

            $tablename = \Config\Sys::$dbprefix.'betslog l,'.\Config\Sys::$dbprefix.'users u';
            $fields = 'l.uid,u.nickname,u.avatar,sum(l.profit) as profits';
            $where = 'l.`state`=1 AND l.`uid`=u.`uid` AND (l.`times` BETWEEN '.$beginTime.' AND '.$endTime.')';
            $gho = 'GROUP BY l.uid HAVING (profits>0) ORDER BY profits DESC LIMIT 20';

            $sql = "select {$fields} from {$tablename} WHERE {$where} {$gho}";
            $data = $db->query($sql);

            # 统计时间 - 缓存5分钟
            $utility->redisSetEx($redisKey, $data, 86400*7);
        }
        return array(
            'state' => 200,
            'datas' => $data,
        );
        */
    }
    # 本月排行榜
    public static function rankingForMonth($paras)
    {
        return array(
            'state' => 200,
            'datas' => array(),
        );
        /*
        global $utility;
        global $redis;
        global $db;

        $data = array();

        $redisKey = \Config\Sys::$redisKey['RANKING'].'month';

        $data = $utility->redisGet($redisKey);

        if(empty($data)){

            $beginTime = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
            $endTime = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));

            $beginTime = strtotime($beginTime);
            $endTime = strtotime($endTime);

            $tablename = \Config\Sys::$dbprefix.'betslog l,'.\Config\Sys::$dbprefix.'users u';
            $fields = 'l.uid,u.nickname,u.avatar,sum(l.profit) as profits';
            $where = 'l.`state`=1 AND l.`uid`=u.`uid` AND (l.`times` BETWEEN '.$beginTime.' AND '.$endTime.')';
            $gho = 'GROUP BY l.uid HAVING (profits>0) ORDER BY profits DESC LIMIT 20';

            $sql = "select {$fields} from {$tablename} WHERE {$where} {$gho}";
            $data = $db->query($sql);

            # 统计时间 - 缓存5分钟
            $utility->redisSetEx($redisKey, $data, 86400*30);
        }
        return array(
            'state' => 200,
            'datas' => $data,
        );
        */
    }
    public static function rankingForYear($paras)
    {
        return array(
            'state' => 200,
            'datas' => array(),
        );
        /*
        global $utility;
        global $redis;
        global $db;

        $data = array();

        $redisKey = \Config\Sys::$redisKey['RANKING'].'month';

        $data = $utility->redisGet($redisKey);

        if(empty($data)){
            $beginTime = date('Y-01-01');
            $endTime = date('Y-12-31');

            $beginTime = strtotime($beginTime);
            $endTime = strtotime($endTime) + 86400;

            $tablename = \Config\Sys::$dbprefix.'betslog l,'.\Config\Sys::$dbprefix.'users u';
            $fields = 'l.uid,u.nickname,u.avatar,sum(l.profit) as profits';
            $where = 'l.`state`=1 AND l.`uid`=u.`uid` AND (l.`times` BETWEEN '.$beginTime.' AND '.$endTime.')';
            $gho = 'GROUP BY l.uid HAVING (profits>0) ORDER BY profits DESC LIMIT 20';

            $sql = "select {$fields} from {$tablename} WHERE {$where} {$gho}";
            $data = $db->query($sql);

            # 统计时间 - 缓存5分钟
            $utility->redisSetEx($redisKey, $data, 86400*365);
        }
        return array(
            'state' => 200,
            'datas' => $data,
        );
        */
    }
    # endregion

    # region 拉取单个的系统参数
    public static function systemParams($paras)
    {
        global $utility;
        //需要处理的页面参数，用来翻页操作及生成缓存键
        $url_args = array(
            'cmd',
            'token',
            'key',
            'remoteip',
        );
        $paras = $utility->get_url_args($paras, $url_args);
        $result = $utility->getSystemParams($paras['key']);
        return array(
            'state' => 200,
            'val' => $result,
        );
    }
    # endregion
}